package com.pandora.component;

import java.io.File;
import java.util.Iterator;

import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.Element;

import com.alibaba.fastjson.JSON;
import com.pandora.exception.UtilException;
import com.pandora.util.XmlUtil;

public class DomainTree {
	private TreeNode treeNode;

	// 构造
	public DomainTree(TreeNode treeNode) {
		this.treeNode = treeNode;
	}

	public String toJson() {
		return JSON.toJSONString(treeNode);
	}

	public void toIbatisXml(String xmlPath) {
		File file=new File(xmlPath);
		try {
			XmlUtil.createXml(file, new DomHandler() {
				@Override
				public void resultCallback(Document doc) {
					DocType docType = new DocType("sqlMap",
							"-//ibatis.apache.org//DTD SQL Map 2.0//EN",
							"http://ibatis.apache.org/dtd/sql-map-2.dtd");
					Element sqlMap = new Element("sqlMap");
					doc.addContent(docType);
					
					doc.setRootElement(sqlMap);

					Element typeAlias = new Element("typeAlias");
					String alias = treeNode.getEnglishName().toLowerCase();
					typeAlias.setAttribute("alias", alias);
					typeAlias.setAttribute("type", treeNode.getClassName());
					sqlMap.addContent(typeAlias);

					Element resultMap = new Element("resultMap");
					resultMap.setAttribute("id", "query" + treeNode.getEnglishName());
					resultMap.setAttribute("class", alias);
					//---------------------------------------------------------------------
					StringBuffer sql = new StringBuffer();
					StringBuffer selectSql = new StringBuffer("select ");
					Element dynamic=new Element("dynamic");
					dynamic.setAttribute("prepend","WHERE");
					
					Iterator<TreeNode> iterator = treeNode.getChildren().iterator();
					while (iterator.hasNext()) {
						TreeNode childTreeNode = iterator.next();
						Element isNotEmpty=new Element("isNotEmpty");
						isNotEmpty.setAttribute("prepend","AND");
						
						String childEnglishName = childTreeNode.getEnglishName();
						Element result = new Element("result");
						result.setAttribute("property", childTreeNode.getPropertyName());
						if (childTreeNode.getClassName().startsWith("java.lang")) {
							result.setAttribute("column", childEnglishName);
							selectSql.append(alias);
							selectSql.append(".");
							selectSql.append(childEnglishName);
							isNotEmpty.setAttribute("property",childEnglishName);
							isNotEmpty.setText(alias+"."+childEnglishName+"=#"+childEnglishName+"#");
						} else {
							result.setAttribute("select",
									"query" + childTreeNode.getEnglishName());
							result.setAttribute("column", childEnglishName + "_id");
							selectSql.append(alias);
							selectSql.append(".");
							selectSql.append(childEnglishName + "_id");
							isNotEmpty.setAttribute("property",childEnglishName+"_id");
							isNotEmpty.setText(alias+"."+childEnglishName+"=#"+childEnglishName+"#");
						}
						if (iterator.hasNext()) {
							selectSql.append(",");
						}
						resultMap.addContent(result);
					}
					sql.append(selectSql);
					sql.append(" from ");
					sql.append(treeNode.getEnglishName());
					sql.append(" ");
					sql.append(alias);
					sqlMap.addContent(resultMap);

					Element select = new Element("select");
					select.setText(sql.toString());
					sqlMap.addContent(select);
				}
			});
		} catch (UtilException e) {
			e.printStackTrace();
		}
	}

	public TreeNode getTreeNode() {
		return treeNode;
	}

	public void setTreeNode(TreeNode treeNode) {
		this.treeNode = treeNode;
	}

}
